import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert' as convert;
class CachService{
  CachService._initData();
  static CachService _instance;
  static _getInstance(){
    if(_instance == null){
      _instance = CachService._initData();
    }
    return _instance;
  }
  factory CachService(){
    return _getInstance();
  }
  setMsg(key,msg) async{
    _setFormateData(key,msg);
  }
  getMsg(msg)async{
    return _formateData(msg);
  }
  removeMsg(msg)async{
    SharedPreferences prefs =await SharedPreferences.getInstance();
    prefs.remove(msg);
  }
  setToken(token) async{
    _setFormateData('_token_',token);
  }
  getToken()async{
    return _formateData('_token_');
  }
  removeToken()async{
    SharedPreferences prefs =await SharedPreferences.getInstance();
    prefs.remove('_token_');
  }
  setUserInfo(userInfo) async{
    _setFormateData('_userInfo_',userInfo);
  }
  getUserInfo()async{
    return _formateData('_userInfo_');
  }
  removeUserInfo()async{
    SharedPreferences prefs =await SharedPreferences.getInstance();
    prefs.remove('_userInfo_');
  }
  clearAllMsg()async{
    SharedPreferences prefs =await SharedPreferences.getInstance();
    prefs.clear();
  }
  _formateData(msg) async{
    SharedPreferences prefs =await SharedPreferences.getInstance();
    var res = prefs.get(msg);
    var jsonObj = convert.jsonEncode(res);
    jsonObj = convert.jsonDecode(jsonObj);
    return jsonObj;
  }
  _setFormateData(key,msg) async{
    SharedPreferences prefs =await SharedPreferences.getInstance();
    String jsonString = convert.jsonEncode(msg);
    print(jsonString);
    prefs.setString(key, jsonString);
  }


}